<template>
  <div class="orders">
    <div class="orders_header">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>订单管理</el-breadcrumb-item>
        <el-breadcrumb-item>订单列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-card shadow="hover" class="orders_con">
      <!-- 搜索框 -->
      <div class="orders_sea">
        <el-input placeholder="请输入内容" clearable @clear="getOrdersList" v-model="queryinfo.query">
          <el-button slot="append" icon="el-icon-search" @click="getOrdersList"></el-button>
        </el-input>
      </div>
      <!-- 列表 -->
      <el-table :data="orderList" style="width: 100%">
        <el-table-column label="#" width="60" type="index">
        </el-table-column>
        <el-table-column label="订单编号" width="400" prop="order_number">
        </el-table-column>
        <el-table-column label="订单价格" width="90" prop="order_price">
        </el-table-column>
        <el-table-column label="是否付款" width="90" prop="pay_status">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status === '1'">已付款</el-tag>
            <el-tag type="danger" v-if="scope.row.pay_status === '0'">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" width="90" prop="is_send">
        </el-table-column>
        <el-table-column label="下单时间">
          <template slot-scope="scope">
            {{gettime(scope.row.create_time)}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"
              @click="handleEdit(scope.row)"></el-button>
            <el-button type="success" size="mini" icon="el-icon-location"
              @click="handleCourier(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block paging">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="queryinfo.pagenum" :page-sizes="[5,8,15]" :page-size="queryinfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
      <!-- 修改地址 -->
      <el-dialog title="修改地址" :visible.sync="dialogChangeVisible" :before-close="handleClose" center
        @close="changecancel">
        <el-form :model="changeaddressform" :rules="changeRules" ref="refchange"
          label-width="100px">
          <el-form-item label="省市区/县">
            <el-cascader v-model="changeaddressform.address1" :options="cityData" :props="cityprop"
              clearable @change="handleChange"></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="email">
            <el-input v-model="changeaddressform.address2" autocomplete="off" clearable></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="changeaddressform = false">取 消</el-button>
          <el-button type="primary" @click="changeDeter">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 物流地址 -->
      <el-dialog title="物流动态" :visible.sync="dialogCourier" width="30%">
        <el-timeline>
          <el-timeline-item v-for="(activity, index) in activities" :key="index">
            {{activity}}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import cityData from './citydata.js'
export default {
  name: 'orDers',
  data() {
    return {
      queryinfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
        user_id: '',
        pay_status: '',
        is_send: '',
        order_fapiao_title: '',
        order_fapiao_company: '',
        order_fapiao_content: '',
        consignee_addr: '',
      },
      // 订单数据列表
      orderList: [],
      // 列表总数
      total: 0,
      // 修改地址框的显示
      dialogChangeVisible: false,
      //  修改地址
      changeaddressform: {
        address1: [],
        address2: '',
      },
      // 修改地址框的规则
      changeRules: {
        address1: [{ required: true, message: '请选择省市地址', trigger: 'blur' }],
        address2: [{ required: true, message: '请输入详细地址', trigger: 'blur' }],
      },
      // 省市数据
      cityData: cityData,
      //  省市选择器规则
      cityprop: {
        value: 'value',
        label: 'label',
        children: 'children',
      },
      //  物流动态框的显示
      dialogCourier: false,
      // 物流数据
      activities: [],
    }
  },
  computed: {
    // 时间戳转换日期
    gettime() {
      return (time) => {
        const date = new Date(time * 1000)
        const Y = date.getFullYear() + '-'
        const M =
          (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
        const D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' '
        const H = date.getHours() + ':'
        const M2 = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
        const S = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
        return Y + M + D + H + M2 + S
      }
    },
  },
  methods: {
    async getOrdersList() {
      const { data: OrderListres } = await this.$http.get('orders', { params: this.queryinfo })
      // console.log(OrderListres)
      this.orderList = OrderListres.data.goods
      this.total = OrderListres.data.total
    },
    // 分页每页数据量
    handleSizeChange(val) {
      this.queryinfo.pagesize = val
      this.getOrdersList()
    },
    // 分页页数
    handleCurrentChange(val) {
      this.queryinfo.pagenum = val
      this.getOrdersList()
    },
    // 点击编辑按钮
    handleEdit() {
      this.dialogChangeVisible = true
    },
    // 关闭修改框前的提示
    handleClose(done) {
      this.$confirm('确认关闭?填写数据将不被保存')
        .then(() => {
          done()
        })
        .catch(() => {})
    },
    // 关闭修改框
    changecancel() {
      this.$refs.refchange.resetField
    },
    // 省市选择器选择后
    handleChange(val) {
      console.log(val)
    },
    // 修改地址框的确定
    changeDeter() {
      this.$refs.refchange.validate((val) => {
        if (!val) {
          this.$message.error('请正确输入修改地址')
          return
        }
        this.$message.success('修改地址成功')
      })
    },
    // 点击物流按钮
    async handleCourier() {
      this.dialogCourier = true
      // const { data: courierres } = await this.$http.get('/kuaidi/arfeesefsfsffw4ttefre')
      // console.log(courierres)
      if (this.activities.length === 0) {
        this.activities.push('暂无数据')
      }
    },
  },
  created() {
    this.getOrdersList()
  },
}
</script>

<style lang='less' scoped>
.orders_header {
  .el-breadcrumb {
    font-size: 17px;
  }
}
.orders_con {
  margin-top: 30px;
  height: 90%;
  background: #fff;
  padding: 20px;
}
.orders_sea {
  width: 560px;
  display: flex;
  justify-content: space-between;
  .el-input {
    width: 80%;
  }
}
.el-table {
  margin-top: 20px;
}
.paging {
  margin-top: 20px;
}
</style>
